#include "MiscEvents.h"
#include "DataTypes.h"

namespace events {

void MiscEvents::saveToStream(std::ostream &os) const {
	os.write(reinterpret_cast<const char*>(&_miscType), sizeof(_miscType));
	switch (_miscType) {
		case MISC_EXIT_PROCESS:
			helpers::saveToStream(os, _info.ep.exitCode);
			break;
		case MISC_DUPLICATE_HANDLE:
			helpers::saveToStream(os, _info.dh.hOrig);
			helpers::saveToStream(os, _info.dh.hCopy);
			break;
	}
}


void MiscEvents::readFromStream(std::istream &is) {
	is.read(reinterpret_cast<char*>(&_miscType), sizeof(_miscType));
	switch (_miscType) {
		case MISC_EXIT_PROCESS:
			helpers::readFromStream(is, &(_info.ep.exitCode));
			break;
		case MISC_DUPLICATE_HANDLE:
			helpers::readFromStream(is, &(_info.dh.hOrig));
			helpers::readFromStream(is, &(_info.dh.hCopy));
			break;
		default:	
			THROW(platform::SerializationException, "unexpected type of MiscEvent");
	}
}

void MiscEvents::writeDebugInfo(std::ostream &os) const {
	os << "MiscEvents: type=";
	switch (_miscType) {
		case MISC_EXIT_PROCESS:
			os << "EXIT_PROCESS, reason=" << getExitProcessCode();
			break;
		case MISC_DUPLICATE_HANDLE:
			os << "DUP_HANDLE, orig=" << getOrigHandle() << ", copy=" << getCopyHandle();
			break;
	}
	os << '\n';
}

}  // end namespace
